home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / ghostscr / gt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-31  |  13.3 KB  |  449 lines

  1. /* Copyright (C) 1989 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gt.c */
  21. /* Test program for GhostScript library */
  22. #include "math_.h"
  23. #include "gx.h"
  24. #include "malloc_.h"
  25. #include "memory_.h"
  26. #include "gsmatrix.h"
  27. #include "gsstate.h"
  28. #include "gscoord.h"
  29. #include "gspaint.h"
  30. #include "gspath.h"
  31. #include "gxdevice.h"
  32.  
  33. int code;
  34. private void e(P2(int, char *));
  35. private void ze(P2(char *, char *));
  36. #define z(ptr,msg) ze((char *)(ptr), msg)
  37.  
  38. private long get_time();
  39.  
  40. /* Parameters set by swproc and argproc */
  41. private int testn = 0;
  42. private int use_null = 0;
  43. private int show_all = 0;
  44. private int repeat = 1;
  45. #define max_params 2
  46. private int params[max_params];
  47. /* Split main and real_main so we can run under a co-linked debugger. */
  48. int real_main(P2(int, char *[]));
  49. main(int argc, char *argv[])
  50. {    return real_main(argc, argv);
  51. }
  52. int
  53. real_main(int argc, char *argv[])
  54. {    int (*proc)(P2(gs_state *, int *));
  55.     gs_state *pgs;
  56.     float ega_scale = 350.0 / (72 * 11);
  57.     float htproc(P2(floatp, floatp));
  58.     int swproc(P2(char, char *));
  59.     void argproc(P2(char *, int));
  60.     memset(params, 0, max_params * sizeof(int));
  61.     gs_main(argc, argv, "GT.MAP", swproc, argproc);
  62.     switch ( testn )
  63.        {
  64. #define set_test(tproc)\
  65.          { int tproc(P2(gs_state *, int *)); proc = tproc; }
  66.     case 1: set_test(test1); break;
  67.     case 2: set_test(test2); break;
  68.     case 3: set_test(test3); break;
  69.     case 4: set_test(test4); break;
  70.     case 6: set_test(test6); break;
  71.     case 99: set_test(test99); break;
  72.     case 100: set_test(test100); break;
  73.     case 101: set_test(test101); break;
  74.     default:
  75.         printf("Unknown test #%d\n", testn);
  76.         exit(1);
  77.        }
  78.     z(pgs = gs_state_alloc(gs_malloc, gs_free), "alloc");
  79.     e(gs_setdevice(pgs, gs_getdevice(0)), "setdevice");
  80.     if ( use_null )
  81.         gx_device_no_output(pgs);    /* suppress output */
  82.     e(gs_setscreen(pgs, 10.0, 45.0, htproc), "setscreen");
  83.     e(gs_initgraphics(pgs), "initgraphics");
  84.     e(gs_erasepage(pgs), "erasepage");
  85.        {    gs_matrix mat;
  86.         gs_make_identity(&mat);
  87.         mat.xx = ega_scale * 48 / 35;
  88.         mat.yy = -ega_scale;
  89.         mat.ty = 350;
  90.         e(gs_setmatrix(pgs, &mat), "setmatrix");
  91.        }
  92.        {    long time1, ttime;
  93.         int count = repeat;
  94.         time1 = get_time();
  95.         do
  96.           { e(gs_gsave(pgs), "outer gsave");
  97.             (*proc)(pgs, params);
  98.             gs_copypage(pgs);
  99.             e(gs_grestore(pgs), "outer grestore");
  100.           }
  101.         while ( --count );
  102.         ttime = get_time() - time1;
  103.         if ( repeat == 1 )
  104.           printf("%ld\n", ttime);
  105.         else
  106.           printf("%ld / %d = %ld\n", ttime, repeat, ttime / repeat);
  107.        }
  108.     getchar();        /* wait for confirmation */
  109. }
  110. float
  111. htproc(floatp x, floatp y)
  112. {    return (float)(1.0 - (x*x + y*y));
  113. }
  114. /* Process switches */
  115. int
  116. swproc(char sw, char *arg)
  117. {    switch ( sw )
  118.        {
  119.     default:
  120.         return -1;
  121.     case 'A':    /* show all steps */
  122.         show_all = 1;
  123.         break;
  124.     case 'N':    /* select null device */
  125.         use_null = 1;
  126.         break;
  127.     case 'R':    /* repeat test (for timing)  */
  128.         sscanf(arg, "%d", &repeat);
  129.         if ( repeat <= 0 )
  130.           {    printf("Repeat count <= 0\n");
  131.             exit(1);
  132.           }
  133.         break;
  134.        }
  135.     return 0;
  136. }
  137. /* Process parameters */
  138. void
  139. argproc(char *arg, int index)
  140. {    if ( index == 0 )
  141.       { sscanf(arg, "%d", &testn);
  142.       }
  143.     else if ( index <= max_params )
  144.       sscanf(arg, "%d", ¶ms[index - 1]);
  145.     else
  146.       { printf("Too many arguments\n");
  147.         exit(1);
  148.       }
  149. }
  150. private void
  151. e(int code, char *str)
  152. {    if ( show_all ) printf("%s\n", str);
  153.     if ( code < 0 )
  154.       { printf("Error, code=%d in %s\n", code, str);
  155.         exit(1);
  156.       }
  157. }
  158. private void
  159. ze(char *ptr, char *str)
  160. {    if ( show_all ) printf("%s\n", str);
  161.     if ( ptr == NULL )
  162.       { printf("Error, result=0 in %s\n", code, str);
  163.         exit(1);
  164.       }
  165. }
  166.  
  167. #define inch(n) (float)((n) * 72)
  168.  
  169. /* A program for testing stroking */
  170. /* 1st parameter is # of degrees to rotate between strokes */
  171. test99(register gs_state *pgs, int *params)
  172. {    int i;
  173.     float ang = params[0];
  174.     float atot = 0;
  175.     if ( ang == 0 ) ang = 30;
  176.     gs_translate(pgs, inch(4), inch(5));
  177.     gs_scale(pgs, inch(3.5), inch(3.5));
  178.     gs_setlinewidth(pgs, 0.1);
  179.     while ( atot < 360 )
  180.        {    gs_moveto(pgs, 0.25, 0.0);
  181.         gs_lineto(pgs, 1.0, 0.0);
  182.         gs_stroke(pgs);
  183.         gs_rotate(pgs, ang);
  184.         atot += ang;
  185.        }
  186. }
  187.  
  188. /* A mandala program for testing stroke speed */
  189. test100(register gs_state *pgs, int *params)
  190. {    int N = max(params[0], 2);
  191.     int delta, i;
  192.     float ang1 = 360.0 / N;
  193.     e(gs_translate(pgs, inch(4.25), inch(5.5)), "translate");
  194.     e(gs_scale(pgs, inch(3.5), inch(3.5)), "scale");
  195.     e(gs_setlinewidth(pgs, 0.0), "setlinewidth");
  196.     for ( delta = 1; delta <= N / 2; delta++ )
  197.        {    float ang = ang1 * delta;
  198.         e(gs_newpath(pgs), "newpath");
  199.         for ( i = 0; i < N; i++ )
  200.            {    e(gs_moveto(pgs, 0.0, 1.0), "moveto");
  201.             e(gs_rotate(pgs, ang), "rotate 1");
  202.             e(gs_lineto(pgs, 0.0, 1.0), "lineto");
  203.             e(gs_rotate(pgs, (float)(ang1 - ang)), "rotate 2");
  204.            }
  205.         e(gs_stroke(pgs), "stroke");
  206.        }
  207. }
  208.  
  209. /* A program for testing colors */
  210. test101(register gs_state *pgs, int *params)
  211. {    int i, j, k;
  212.     float unit = 36.0;
  213.     for ( i = 0; i <= 4; i++ )
  214.       for ( j = 0; j <= 4; j++ )
  215.         for ( k = 0; k <= 4; k++ )
  216.           { e(gs_setrgbcolor(pgs, i * 0.25, j * 0.25, k * 0.25), "setrgbcolor");
  217.         gs_newpath(pgs);
  218.         gs_moveto(pgs, (j * 5 + k + 0.1) * unit, (i + 0.1) * unit);
  219.         gs_rlineto(pgs, 0.0, unit);
  220.         gs_rlineto(pgs, unit, 0.0);
  221.         gs_rlineto(pgs, 0.0, -unit);
  222.         gs_closepath(pgs);
  223.         gs_fill(pgs);
  224.           }
  225. }
  226.  
  227. /* ------ Programs from the PostScript Cookbook ------ */
  228.  
  229. /* Program 1 from the PostScript Cookbook */
  230. /* 1st parameter gives # of petals, default is 3 */
  231. test1(register gs_state *pgs, int *params)
  232. {    void wedge(P2(gs_state *, floatp));
  233.     int i;
  234.     int param = params[0];
  235.     int nseg = (abs(param) < 3 ? (param = 3) : abs(param));
  236.     float ang = 360.0 / nseg;
  237.     float ang2 = ang / 2;
  238.     e(gs_gsave(pgs), "gsave 1");
  239.     e(gs_translate(pgs, inch(3.75), inch(7.25)), "translate");
  240.     e(gs_scale(pgs, inch(1), inch(1)), "scale");
  241.     wedge(pgs, ang2);
  242.     e(gs_setlinewidth(pgs, 0.02), "setlinewidth");
  243.     e(gs_stroke(pgs), "stroke");
  244.     e(gs_grestore(pgs), "grestore 1");
  245.     e(gs_gsave(pgs), "gsave 2");
  246.     gs_translate(pgs, inch(4.25), inch(4.25));
  247.     gs_scale(pgs, inch(1.75), inch(1.75));
  248.     gs_setlinewidth(pgs, 0.02);
  249.     for ( i = 1; i <= nseg; i++ )
  250.        {    gs_setgray(pgs, (float)i / param);
  251.         if ( param < 0 ) gs_rotate(pgs, -ang);
  252.         e(gs_gsave(pgs), "gsave 3");
  253.         wedge(pgs, ang2);
  254.         e(gs_gsave(pgs), "gsave 4");
  255.         gs_fill(pgs);
  256.         e(gs_grestore(pgs), "grestore 4");
  257.         gs_setgray(pgs, 0.0);
  258.         gs_stroke(pgs);
  259.         e(gs_grestore(pgs), "grestore 3");
  260.         if ( param > 0 ) gs_rotate(pgs, ang);
  261.        }
  262.     e(gs_grestore(pgs), "grestore 2");
  263. }
  264. void
  265. wedge(register gs_state *pgs, floatp ang)
  266. {    float asin = sin(ang * M_PI / 180.0);
  267.     e(gs_moveto(pgs, 0.0, 0.0), "moveto");
  268.     e(gs_translate(pgs, 1.0, 0.0), "translate 1");
  269.     e(gs_rotate(pgs, (float)ang), "rotate");
  270.     e(gs_translate(pgs, 0.0, asin), "translate 2");
  271.     e(gs_arc(pgs, 0.0, 0.0, asin, -90.0, 90.0), "arc");
  272.     e(gs_closepath(pgs), "closepath");
  273. }
  274.  
  275. /* Program 2 from the PostScript Cookbook */
  276. /* 1st parameter specifies a rotation amount */
  277. /* If 2nd parameter is non-zero, trace square in opposite direction */
  278. test2(gs_state *pgs, int *params)
  279. {    void centersquare(P2(gs_state *, int));
  280.     gs_matrix cmtx;
  281.     int i;
  282.     float rota = params[0];
  283.     int cw = params[1];
  284.     e(gs_gsave(pgs), "gsave 1");
  285.     e(gs_translate(pgs, inch(2.5), inch(6)), "translate 1");
  286.     e(gs_setlinewidth(pgs, 1.0 / 16), "setlinewidth 1");
  287.     if ( rota != 0 )
  288.         e(gs_rotate(pgs, rota), "rotate");
  289.     for ( i = 1; i <= 5; i++ )
  290.        {    e(gs_gsave(pgs), "gsave 2");
  291.         e(gs_scale(pgs, i * inch(0.5), i * inch(0.5)), "scale 1");
  292.         centersquare(pgs, cw);
  293.         e(gs_stroke(pgs), "stroke 1");
  294.         e(gs_grestore(pgs), "grestore 2");
  295.        }
  296.     e(gs_grestore(pgs), "grestore 1");
  297.     e(gs_gsave(pgs), "gsave 3");
  298.     e(gs_translate(pgs, inch(6), inch(6)), "translate 2");
  299.     e(gs_setlinewidth(pgs, 1.0), "setlinewidth 2");
  300.     e(gs_currentmatrix(pgs, &cmtx), "currentmatrix");
  301.     for ( i = 1; i <= 5; i++ )
  302.        {    e(gs_gsave(pgs), "gsave 4");
  303.         e(gs_scale(pgs, i * inch(0.5), i * inch(0.5)), "scale 2");
  304.         centersquare(pgs, cw);
  305.         e(gs_setmatrix(pgs, &cmtx), "setmatrix");
  306.         e(gs_stroke(pgs), "stroke 2");
  307.         e(gs_grestore(pgs), "grestore 4");
  308.        }
  309.     e(gs_grestore(pgs), "grestore 3");
  310. }
  311. void
  312. centersquare(register gs_state *pgs, int cw)
  313. {    float d = (cw ? 0.5 : -0.5);
  314.     e(gs_newpath(pgs), "newpath");
  315.     e(gs_moveto(pgs, 0.5, 0.5), "moveto");
  316.     e(gs_lineto(pgs, d, -d), "lineto 1");
  317.     e(gs_lineto(pgs, -0.5, -0.5), "lineto 2");
  318.     e(gs_lineto(pgs, -d, d), "lineto 3");
  319.     e(gs_closepath(pgs), "closepath");
  320. }
  321.  
  322. /* Program 3 from the PostScript Cookbook */
  323. test3(register gs_state *pgs, int *params)
  324. {    void ellipse(P7(gs_state *, int, int, int, int, int, int));
  325.     e(gs_newpath(pgs), "newpath 1");
  326.     ellipse(pgs, 144, 400, 72, 144, 0, 360);
  327.     e(gs_stroke(pgs), "stroke 1");
  328.     e(gs_newpath(pgs), "newpath 2");
  329.     ellipse(pgs, 400, 400, 144, 36, 0, 360);
  330.     e(gs_fill(pgs), "fill 2");
  331.     e(gs_newpath(pgs), "newpath 3");
  332.     ellipse(pgs, 300, 180, 144, 72, 30, 150);
  333.     e(gs_stroke(pgs), "stroke 3");
  334.     e(gs_newpath(pgs), "newpath 4");
  335.     ellipse(pgs, 480, 150, 30, 50, 270, 90);
  336.     e(gs_fill(pgs), "fill 4");
  337. }
  338. void
  339. ellipse(register gs_state *pgs,
  340.   int x, int y, int xrad, int yrad, int startangle, int endangle)
  341. {    gs_matrix savematrix;
  342.     e(gs_currentmatrix(pgs, &savematrix), "currentmatrix");
  343.     e(gs_translate(pgs, (float)x, (float)y), "translate");
  344.     e(gs_scale(pgs, (float)xrad, (float)yrad), "scale");
  345.     e(gs_arc(pgs, 0.0, 0.0, 1.0, (float)startangle, (float)endangle), "arc");
  346.     e(gs_setmatrix(pgs, &savematrix), "setmatrix");
  347. }
  348.  
  349. /* Program 4 from the PostScript Cookbook */
  350. test4(register gs_state *pgs, int *params)
  351. {    void arrow(P8(gs_state *, int, int, int, int, int, int, floatp));
  352.     e(gs_newpath(pgs), "newpath 1");
  353.     arrow(pgs, 318, 340, 72, 340, 10, 30, 72.0);
  354.     e(gs_fill(pgs), "fill 1");
  355.     e(gs_newpath(pgs), "newpath 2");
  356.     arrow(pgs, 382, 400, 542, 560, 72, 232, 116.0);
  357.     e(gs_setlinewidth(pgs, 3.0), "setlinewidth");
  358.     e(gs_stroke(pgs), "stroke");
  359.     e(gs_newpath(pgs), "newpath 3");
  360.     arrow(pgs, 400, 300, 400, 90, 90, 200, 200 * 1.732 / 2);
  361.     e(gs_setgray(pgs, 0.65), "setgray");
  362.     e(gs_fill(pgs), "fill 2");
  363. }
  364. void
  365. arrow(register gs_state *pgs,
  366.   int tailx, int taily, int tipx, int tipy, int thickness, int headthickness,
  367.   floatp hl)
  368. {    float ht = (float)thickness / 2;
  369.     float hht = (float)headthickness / 2;
  370.     float dx = tipx - tailx;
  371.     float dy = tipy - taily;
  372.     float al = sqrt(dx * dx + dy * dy);
  373.     float angle = atan2(dy, dx) * (180.0 / M_PI);
  374.     float base = al - hl;
  375.     gs_matrix savematrix;
  376.     e(gs_currentmatrix(pgs, &savematrix), "currentmatrix");
  377.     e(gs_translate(pgs, (float)tailx, (float)taily), "translate");
  378.     e(gs_rotate(pgs, angle), "rotate");
  379.     e(gs_moveto(pgs, 0.0, (float)-ht), "moveto");
  380.     e(gs_lineto(pgs, base, (float)-ht), "line 1");
  381.     e(gs_lineto(pgs, base, (float)-hht), "line 2");
  382.     e(gs_lineto(pgs, al, 0.0), "line 3");
  383.     e(gs_lineto(pgs, base, hht), "line 4");
  384.     e(gs_lineto(pgs, base, ht), "line 5");
  385.     e(gs_lineto(pgs, 0.0, ht), "line 6");
  386.     e(gs_closepath(pgs), "closepath");
  387.     e(gs_setmatrix(pgs, &savematrix), "setmatrix");
  388. }
  389.  
  390. /* Program 6 from the PostScript Cookbook */
  391. /* (actually, the test program for the imagemask operator */
  392. /* that appears in the PostScript reference manual) */
  393. /* First parameter is amount to rotate image */
  394. /* If second parameter is non-zero, clip the image */
  395. int ii;
  396. test6(register gs_state *pgs, int *params)
  397. {    gs_matrix mat;
  398.     int i6proc(P2(byte **, int *));
  399.     e(gs_translate(pgs, inch(3), inch(4)), "translate");
  400.     e(gs_scale(pgs, inch(2), inch(2)), "scale");
  401.     gs_moveto(pgs, 0.0, 0.0);
  402.     gs_lineto(pgs, 0.0, 1.0);
  403.     gs_lineto(pgs, 1.0, 1.0);
  404.     gs_lineto(pgs, 1.0, 0.0);
  405.     gs_closepath(pgs);
  406.     gs_setgray(pgs, 0.9);
  407. /***    gs_fill(pgs);    ***/
  408.     gs_setgray(pgs, 0.4);
  409.     /* The following is not in the original program. */
  410.     /* It is here to test clipping of images. */
  411.     if ( params[1] )
  412.        {    e(gs_newpath(pgs), "newpath");
  413.         e(gs_moveto(pgs, 0.0, 0.0), "moveto");
  414.         e(gs_lineto(pgs, 1.0, 3.0), "line1");
  415.         e(gs_lineto(pgs, 3.0, 1.0), "line2");
  416.         e(gs_closepath(pgs), "closepath");
  417.         e(gs_clip(pgs), "clip");
  418.        }
  419.     e(gs_rotate(pgs, (float)params[0]), "rotate");
  420.     gs_make_identity(&mat);
  421.     mat.xx = 24;
  422.     mat.yy = -23;
  423.     mat.ty = 23;
  424.     ii = 0;
  425.     e(gs_imagemask(pgs, 24, 23, 1, &mat, i6proc), "image");
  426. }
  427. private byte i6data[3 * 23] =
  428.    {    0x00,0x3b,0x00, 0x00,0x27,0x00, 0x00,0x24,0x80, 0x0e,0x49,0x40,
  429.     0x11,0x49,0x20, 0x14,0xb2,0x20, 0x3c,0xb6,0x50, 0x75,0xfe,0x88,
  430.     0x17,0xff,0x8c, 0x17,0x5f,0x14, 0x1c,0x07,0xe2, 0x38,0x03,0xc4,
  431.     0x70,0x31,0x82, 0xf8,0xed,0xfc, 0xb2,0xbb,0xc2, 0xbb,0x6f,0x84,
  432.     0x31,0xbf,0xc2, 0x18,0xea,0x3c, 0x0e,0x3e,0x00, 0x07,0xfc,0x00,
  433.     0x03,0xf8,0x00, 0x1e,0x18,0x00, 0x1f,0xf8,0x00
  434.    };
  435. int
  436. i6proc(byte **pdata, int *psize)
  437. {    *pdata = &i6data[ii++ * 23];
  438.     *psize = 23;
  439.     return 0;
  440. }
  441.  
  442. /* Read the current time (in milliseconds since midnight). */
  443. private long
  444. get_time()
  445. {    long date_time[2];
  446.     gs_get_clock(date_time);
  447.     return date_time[0] * 86400000L + date_time[1];
  448. }
  449.